/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.security.plugin;

import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.spring.provider.plugin.impl.AbstractSimpleSpringProviderPlugin;

/**
 * The Plugin that allow to automatically declares Spring beans.
 */
public class SpringProviderPlugin
    extends AbstractSimpleSpringProviderPlugin
{

  /**
   * the qualified name of the web plugin class.
   */
  private static final String WEB_PLUGIN_QUALIFIED_NAME = "com.genia.toolbox.web.spring.plugin.WebSpringProviderPlugin";

  /**
   * the qualified name of the WSS4J In hanlder for webservice security.
   */
  private static final String CXF_SECURITY_WSS4J_IN_INTERCEPTOR_QUALIFIED_NAME = "org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor";

  /**
   * the main config file.
   */
  private static final String MAIN_CONFIG_FILE_RESOURCE = "/com/genia/toolbox/security/plugin/main-spring-config.xml";

  /**
   * the config file for web related beans.
   */
  private static final String SPRING_SECURITY_WEB_CONFIG_FILE_RESOURCE = "/com/genia/toolbox/security/plugin/web-spring-config.xml";

  /**
   * the spring config file to add Ws Security with spring security.
   */
  private static final String XFIRE_SECURITY_SPRING_SECURITY_CONFIG_FILE_RESOURCE = "/com/genia/toolbox/security/plugin/webservice-spring-config.xml";



  /**
   * Constructor.
   */
  public SpringProviderPlugin()
  {
    super(getConfigFiles());
  }



  /**
   * returns the config file to load.
   * 
   * @return the config file to load
   */
  private static String[] getConfigFiles()
  {
    List<String> configFiles = new ArrayList<String>();
    configFiles.add(MAIN_CONFIG_FILE_RESOURCE);
    if (hasClass(WEB_PLUGIN_QUALIFIED_NAME)) {
      configFiles.add(SPRING_SECURITY_WEB_CONFIG_FILE_RESOURCE);
      if (hasClass(CXF_SECURITY_WSS4J_IN_INTERCEPTOR_QUALIFIED_NAME)) {
        configFiles.add(XFIRE_SECURITY_SPRING_SECURITY_CONFIG_FILE_RESOURCE);
      }
    }
    return configFiles.toArray(new String[configFiles.size()]);
  }



  /**
   * returns whether the class which qualified name is given in parameter is in
   * the classpath.
   * 
   * @param className
   *          the qualified name of the class to search
   * @return <code>true</code> if and only if the class which qualified name
   *         is given in parameter is in the classpath
   */
  private static boolean hasClass(String className)
  {
    try {
      Class.forName(className);
      return true;
    }
    catch (ClassNotFoundException e) {
      // No such class
    }
    return false;
  }

}
